const Actions = require('./actions');
const DB = require('app/db.js');
import { Storage } from 'clientConfig/util/StoreData'
import { Toast } from 'saltui';
import { Control } from 'react-keeper';
import { ready } from 'clientConfig/util/queryurlfield'
const device = require('clientConfig/util/jsapi/device');
module.exports = Reflux.createStore({
    listenables: [Actions],
    data: {

    },
    onFetch: function (params, cb) {
        let t = this;
        let { grade, term, pitchId } = params
        DB.Parent.get_generalClass_detail({
            schoolId: Storage.get('user.schoolId'),
            studentId: Storage.get('user.studentId'),
            grade,
            term,
            pitchId
        })
            .then((content) => {
                if (content.code == 1) {
                    if (content && content.data) {
                        t.data.listData = content.data.list;
                        t.updateComponent();
                    }
                }else{
                    Toast.show({
                        type: 'error',
                        content: '系统异常'
                    });
                }
            })
            .catch(function (error) {
                Toast.show({
                    type: 'error',
                    content: '系统异常'
                });
            });
    },
    onAppendClass: function (params, cb) {
        let t = this;
        let { classEleId } = params
        DB.Parent.append_general_class({
            schoolId: Storage.get('user.schoolId'),
            studentId: Storage.get('user.studentId'),
            classEleId,
        })
            .then((content) => {
                if (content.code == 1) {
                    if (content && content.data) {
                        Control.go(-1)
                    }
                } else {
                    Toast.show({
                        type: 'error',
                        content: '系统异常'
                    });
                }
            })
            .catch(function (error) {
                Toast.show({
                    type: 'error',
                    content: '系统异常'
                });
            });
    },
    //普通选课已选课详情
    onFetchDetail: function (params, cb) {
        let t = this;
        let { classEleId } = params
        DB.Parent.get_generalClassSelected_detail({
            schoolId: Storage.get('user.schoolId'),
            classEleId,
        })
            .then((content) => {
                if (content.code == 1) {
                    if (content && content.data) {
                        Object.assign(t.data, content.data)
                        t.updateComponent();
                    }
                } else {
                    Toast.show({
                        type: 'error',
                        content: '系统异常'
                    });
                }
            })
            .catch(function (error) {
                Toast.show({
                    type: 'error',
                    content: '系统异常'
                });
            });
    },
    //普通选课审核状态退课
    onRemoveGeneralCourse: function (params, cb) {
        let t = this;
        let { eleStuId } = params;
        ready(() => { device.showPreloader() })
        DB.Parent.remove_general_course({
            schoolId: Storage.get('user.schoolId'),
            eleStuId,
        })
            .then((content) => {
                ready(() => { device.hidePreloader() })
                if (content.code == 1) {
                    Control.go(-1);
                } else {
                    Toast.show({
                        type: 'error',
                        content: '系统异常'
                    });
                }
            })
            .catch(function (error) {
                Toast.show({
                    type: 'error',
                    content: '系统异常'
                });
            });
    },
    updateComponent: function () {
        this.trigger(this.data);
    }
});
